﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Reflection;
using InsideDL.MISService;
using InsideDL.MISControl;
using InsideDL.MISData;

namespace InsideDL
{
    public partial class frmTicket : MISControl.frmMatrix 
    {
        protected MISService.VerifyMatrix verifyObj = null;
        protected object[] ftpPara = null;
        protected EXLAPP exlAPP = null;
        protected string saveFilePath
        {
            get
            {
                return ucALMFilePath.Txt;
            }
        }        

        public frmTicket()
        {
            InitializeComponent();
            //vt = new VerifyTickey();
            this.controlList = new List<ucSelFileOrPath>
            {
                ucALMFilePath,ucTicketFile
            };
            ucExecute1.ExecuteClick += new EventHandler(ucClickFacade);
            ucExecute_Exit.ExecuteClick += new EventHandler(ucClickFacade);
        }

        protected override void execute()
        {
            Utility.LoadConfigValue("FTPCollection");
            ftp = Utility.GetConfigValue("ftp");
            id = Utility.GetConfigValue("ftpid");
            pw = Utility.GetConfigValue("ftppw");

            if (base.Vaildation().Equals(false))
            { return; }

            if (!checkFileInFTPSite())
            { return; }
            //產生出 CSV 要用的 Schema
            DataInside di = new DataInside();
            //di.GetCSVFields(downList, ucALMFilePath.Txt);
            Utility.DownloadFile(ftp, id, pw, downList, saveFilePath);   
            Program.CreateFile(saveFilePath + "\\schema.ini",
               Utility.GetARTMACDSchema(downList, di.GetCSVFields(downList,
               saveFilePath)));
                        
            DataTicket tickData = new DataTicket();
            
            object dt = tickData.GetDataRS(downList, saveFilePath
                , Utility.GetThisLastMonth(downList[0])
                , Utility.GetCurrentDay(downList[0]));

            string flName = string.Format("{0}債票券"
              , Utility.GetTWYM());

            createNewFile(flName, System.IO.Path.GetDirectoryName(ucTicketFile.Txt)
                        , ucTicketFile.Txt);


            exlAPP = new EXLAPP();
            
            exlAPP.OpenFile(newFile);
            exlAPP.CopyFromRecordset((ADODB.RecordsetClass)dt, "A2:G10000"
                , "A2:G10000", "I資料");

            string[] refreshTables = new[] { "資料(海外)","資料(台.外幣)"};
            foreach (string rf in refreshTables)
            {
                exlAPP.RefreshPivotTables(rf);
            }
            //「量率(台外幣)」工作表 B1欄位改當月天數
            string day = Utility.GetCurrentDay(downList[0]);
            exlAPP.SetRangeValue("量率 (台外幣)", "B1", day);
            exlAPP.SetRangeValue("量率 (台外幣)", "C2", Utility.GetTWYM());
            exlAPP.SetRangeValue("I資料", "I1"
                ,string.Format("'{0}", Utility.GetTWYM()));
            exlAPP.SaveCloseXLS();
            showMessage(this.Text, "完成!");
        }
        
        //protected void createNewFile()
        //{
        //    //也許可以用Path.Combine
        //    newFile = string.Format(@"{1}\{0}債票券.xls"
        //        , Utility.GetTWYM()
        //        , System.IO.Path.GetDirectoryName(ucTicketFile.Txt)
        //        );
        //    File.Copy(ucTicketFile.Txt, newFile, true);
        //    //lastExeceFile = target;
        //}

        protected bool checkFileInFTPSite()
        {
            verifyObj = new InsideDL.MISService.VerifyTickey();
            ftpPara = new object[]{ Utility.GetCurrentYM(),
            Utility.GetFTPFileList(ftp, id, pw) };
            bool ok = verifyObj.CheckFileInFtp(ftpPara);
            if (ok == true)
            {
                downList = verifyObj.GetDownList();
            }
            return ok;
        }
    }
}
